package com.ums.scumspay.sync;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ums.scumspay.constant.PayOrderNoConstant;
import com.ums.scumspay.entity.PayOrder;
import com.ums.scumspay.mapper.PayOrderMapper;
import com.ums.scumspay.service.BtoCService;
import com.ums.scumspay.service.CtoBService;
import com.ums.scumspay.service.H5PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentMap;

@Component
@EnableScheduling
public class SyncQueryPay {

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Autowired
    private BtoCService  btoCService;

    @Autowired
    private CtoBService ctoBService;

    @Autowired
    private H5PayService h5PayService;

    // @Scheduled(fixedDelay = 20000)
    public void payQuery(){
        ConcurrentMap<String, Map<String,Integer>> map = PayOrderNoConstant.getMap();
        if (map!=null){
            if (map.containsKey("b2c")&& map.get("b2c")!=null){
                Map<String,Integer> list = map.get("b2c");
                for (String s : list.keySet()) {
                    if (list.get(s)>=2){
                        list.remove(s);
                        continue;
                    }
                    String[] split = s.split("-");
                    PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getTransNo, split[0]));
                    if (payOrder.getPayStatus().equals("00") || payOrder.getPayStatus().equals("02")){
                        list.remove(s);
                        continue;
                    }
                    Date parse = null;
                    try {
                        parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(payOrder.getTransDate() + " " + payOrder.getTransTime());
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    if (ChronoUnit.SECONDS.between(LocalDateTime.ofInstant(parse.toInstant(), ZoneId.systemDefault()), LocalDateTime.now())>15){
                        Map<String, String> result=null;
                        if (!split[1].equals("3")){
                            result = btoCService.sendQueryPayResult(split[0]);
                        }else {
                            result = btoCService.sendQueryPayResult(split[0]);
                        }
                        if (result!=null && result.containsKey("tradeCode") && result.get("tradeCode").equals("00")){
                            payOrder.setPayStatus("00");
                            payOrder.setRefundSign("0");
                            payOrder.setProof(result.get("traceNo"));
                            payOrderMapper.updateById(payOrder);
                            list.remove(s);
                        }else {
                            Integer integer = list.get(s)+1;
                            list.put(s,integer);
                        }
                    }
                }
            }else if (map.containsKey("c2b") && map.get("c2b")!=null){
                Map<String,Integer> list = map.get("c2b");
                for (String s : list.keySet()) {
                    if (list.get(s)>=2){
                        list.remove(s);
                        continue;
                    }
                    String[] split = s.split("-");
                    PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getTransNo, split[0]));
                    if (payOrder.getPayStatus().equals("00") || payOrder.getPayStatus().equals("02")){
                        list.remove(s);
                        continue;
                    }
                    Date parse = null;
                    try {
                        parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(payOrder.getTransDate() + " " + payOrder.getTransTime());
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    if (ChronoUnit.SECONDS.between(LocalDateTime.ofInstant(parse.toInstant(), ZoneId.systemDefault()), LocalDateTime.now())>15){
                        Map<String, String> result = ctoBService.sendQueryPayResult(split[0]);
                        if (result!=null && result.containsKey("billStatus") &&  result.get("billStatus").equals("PAID")){
                            payOrder.setPayStatus("00");
                            payOrder.setRefundSign("0");
                            // payOrder.setProof(result.get("traceNo"));
                            payOrderMapper.updateById(payOrder);
                            list.remove(s);
                        }else {
                            Integer integer = list.get(s)+1;
                            list.put(s,integer);
                        }
                    }
                }
            }else if (map.containsKey("h5")&& map.get("h5")!=null){
                Map<String,Integer> list = map.get("h5");
                for (String s : list.keySet()) {
                    if (list.get(s)>=2){
                        list.remove(s);
                        continue;
                    }
                    String[] split = s.split("-");
                    PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getTransNo, split[0]));
                    if (payOrder.getRefundSign().equals("0") || payOrder.getRefundSign().equals("1")){
                        list.remove(s);
                        continue;
                    }
                    Date parse = null;
                    try {
                        parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(payOrder.getTransDate() + " " + payOrder.getTransTime());
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    if (ChronoUnit.SECONDS.between(LocalDateTime.ofInstant(parse.toInstant(), ZoneId.systemDefault()), LocalDateTime.now())>15){
                        Map<String, String> result = h5PayService.sendQueryPayResult(split[0]);
                        if (result!=null && result.get("status").equals("TRADE_SUCCESS")){
                            payOrder.setPayStatus("00");
                            payOrder.setRefundSign("0");
                            // payOrder.setProof(result.get("traceNo"));
                            payOrderMapper.updateById(payOrder);
                            list.remove(s);
                        }else {
                            Integer integer = list.get(s)+1;
                            list.put(s,integer);
                        }
                    }
                }
            }
        }
    }



    public static void main(String[] args) throws InterruptedException, ParseException {
        HashMap<String, Map<String, List<String>>> map = new HashMap<>();
        HashMap<String, List<String>> map1 = new HashMap<>();
        map1.put("1",Arrays.asList("ss"));
        map.put("1", map1);
        System.out.println(map);
        List<String> list = map.get("1").get("1");
        System.out.println(list);
        // list.remove(0);
        list.remove(0);
        System.out.println(map);
    }
}
